import { message } from "antd"
import {
  getProceedsConfig,
  updateProceedsConfig

}  from '../services/chargeCode';


export default {
  namespace: 'chargeCode',

  state: {
    codeItem:{}
  },

  effects: {
    *getProceedsConfig({payload}, { call, put }) {
      const data = yield call(getProceedsConfig);
      if(data.status == 0){
        yield put({
          type: 'updateState',
          payload: {
            codeItem:data.result
          }
        })
      }else{
       message.error(`${data.message}`)
      }
    },

    *updateProceedsConfig({payload}, { call, put }) {
      const data = yield call(updateProceedsConfig,payload);
      if(data.status == 0){
        message.success("编辑成功!")
         yield put({type:"getProceedsConfig"})
      }else{
        message.error(`${data.message}`)
      }
    },




  },

  reducers: {
    updateState (state, { payload }) {
      return {
        ...state,
        ...payload,
      }
    },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(location => {
        if (location.pathname === "/charge/code") {
          dispatch({ type: "getProceedsConfig" });

        }
      });
    }
  },

};
